package com.example.studentinfo.config;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.io.File;
import java.util.List;

/**
 * <p>
 * 测试生成代码
 */
public class MybatisPlusGenerator {

    /**
     * 表前缀
     */
    private static final String TABLE_PREFIX = "";

    private static final String PACKAGE_NAME = "com.example.studentinfo";
    private static final String MODULE_NAME = "api";

    private static final DataSourceConfig DB = getDevDb();

    private static final String[] TABLE_NAME = {
            "student"
    };

    public static void main(String[] args) {
        generateByTables();
    }


    private static void generateByTables() {
        StrategyConfig strategyConfig = new StrategyConfig.Builder()
                .enableCapitalMode()
                .addInclude(MybatisPlusGenerator.TABLE_NAME)
                .disableSqlFilter()
                .addTablePrefix(TABLE_PREFIX)
                .controllerBuilder()
                .enableRestStyle()
                .enableHyphenStyle()
                .entityBuilder()
                .addTableFills(List.of(new Column("create_time", FieldFill.INSERT), new Column("update_time", FieldFill.INSERT_UPDATE)))
                .naming(NamingStrategy.underline_to_camel)
                .logicDeleteColumnName("del_flag")
                .enableActiveRecord()
                .enableLombok()
                .enableFileOverride()
                .mapperBuilder()
                .enableBaseResultMap()
                .serviceBuilder()
                .convertServiceFileName(entityName -> entityName + "Service")
                .build();

        String srcPath = "src" + File.separator + "main" + File.separator + "java";

        GlobalConfig globalConfig = new GlobalConfig.Builder()
                .author(System.getProperty("user.name"))
                .outputDir(srcPath)
                .disableOpenDir()
                .build();

        new AutoGenerator(DB)
                .global(globalConfig)
                .strategy(strategyConfig)
                .packageInfo(new PackageConfig.Builder()
                        .parent(PACKAGE_NAME)
                        .controller("controller")
                        .entity("entity")
                        .moduleName("api")
                        .build())
                .execute();
    }


    private static DataSourceConfig getDevDb() {
        String dbUrl = "jdbc:mysql://localhost:3306/student?tinyInt1isBit=false&useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        return new DataSourceConfig.Builder(dbUrl, "root", "root").build();
    }


}